package com.bt.dmdb.json.converter;

import java.io.IOException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class JsonArrayToString extends JsonDeserializer<String> {

    private static final Logger log = LoggerFactory.getLogger(JsonArrayToString.class);

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String[] valArray = jp.readValueAs(String[].class);
        if (ArrayUtils.isNotEmpty(valArray)) {
            String output = StringUtils.join(valArray, ',');
            log.trace("Deserialized Output: {}", output);
            return output;
        } else {
            log.trace("Deserialized Output: null");
            return null;
        }
    }
}
